﻿using System;
using System.Windows;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core.UI;
using Microsoft.Practices.Unity;
using Baosight.ZNH.ZMCS.UI.ChildWindows;
using Telerik.Windows.Controls;
using Baosight.ZNH.ZMCS.UI.Utils;
using System.Collections.Generic;
using Baosight.ZNH.ZMCommon.ChildWindows;
using System.Windows.Controls;
using Telerik.Windows.Controls.GridView;

//对应ServiceZMCS02.java
namespace Baosight.ZNH.ZMCS.UI
{
    /// <summary>
    /// 系统模块显示
    /// </summary>
    /// Title:ZMCS02,ServiceZMCS02
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 张志芳
    /// Version: 1.0
    /// History:
    ///       2010-12-14  张志芳   [创建]
    ///       2011-04-22  严洪浩   [修改新增的模式为弹出]
    ///
    public partial class ZMCS02 : EFViewCmes
    {
        private SystemModuleBlock block = new SystemModuleBlock();//构造前台下拉列表

        public ZMCS02()
        {
            InitializeComponent();
        }

        public override void Initialize(EIInfo initData)
        {
            initData.AddBlock(block.GetModuleLevels());//列表区 模块级别
            initData.AddBlock(block.GetStatus(true));//查询区 模块状态
            base.Initialize(initData);

            this.INQU_fdStatus.Value = "";//查询区 初始数据
            base.BeforeAction += OnBeforeAction;
        }

        /// <summary>
        /// 列表中修改、删除前设置属性、参数
        /// 传递到后台代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();

            //删除前传递参数
            _view.Args.Add("fdParentId", this.fdParentId.GetDataValue().ToString());
            _view.Args.Add("fdParentName", this.fdParentName.GetDataValue().ToString());
            _view.Args.Add("fdParentLevel", this.fdParentLevel.GetDataValue().ToString());
            _view.Args.Add("isLeaf", this.isLeaf.GetDataValue().ToString());

            base.ViewProperty = _view;
        }

        /// <summary>
        /// 点击新增按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            EIInfo eiInfo = new EIInfo();
            base.OnInsertButtonClickPopup(eiInfo, "系统模块-新增", "insert", null, sender, e);
        }

        /// <summary>
        /// 点击修改按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            IList<EIRow> _list = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();

            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            EIRow row = new EIRow();
            if (_list.Count > 0)
            {
                row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
            }
            eIBlock.SetRow(0, row);
            eiInfo.AddBlock(eIBlock);

            base.OnUpdateButtonClickPopup(eiInfo, "系统模块-修改", "update", null, sender, e);
        }

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CmesWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                this.grid.Refresh();//刷新Grid区域
                this.UpdateHomePageData(new EIInfo());//发布数据
            }

            //按钮生效
            base.TempButtonOpen(true);

            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }

        /// <summary>
        /// 点击启用事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnModifyStartButtonClick(object sender, RoutedEventArgs e)
        {
            base.OnModifyButtonClick("modifyStart", "启用", sender, e);
        }

        /// <summary>
        /// 点击停用事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnModifyStopButtonClick(object sender, RoutedEventArgs e)
        {
            base.OnModifyButtonClick("modifyStop", "停用", sender, e);
        }

        /// <summary>
        /// 明细
        /// By 严洪浩 2011.4.20 10:41
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton link = sender as HyperlinkButton;

            EIRow row = (link.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();
            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            eIBlock.SetRow(0, row == null ? new EIRow() : row);
            eiInfo.AddBlock(eIBlock);

            base.OnHyperlinkButtonClickPopup(eiInfo, "系统模块-查看", "detail", null, sender, e);
        }

        /// <summary>
        /// 重写基类方法：根据数据的改变更新主页面的数据
        /// </summary>
        /// <param name="data"></param>
        public override void UpdateHomePageData(EIInfo data)
        {
            // 数据发布
            IEventAggregator eventAggreator = Container.Resolve<IEventAggregator>();
            EIDataPublishedEvent dataPublishedEvent = eventAggreator.GetEvent<EIDataPublishedEvent>();
            dataPublishedEvent.Publish(data);
        }

        /// <summary>
        /// 新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            SystemModule systemModule = new SystemModule(_dialogType);//构造一个新的修改页面
            systemModule.EIDataContext = new Platform.Core.UI.EIDataContext();

            _eIInfo.AddBlock(block.GetStatus(false));//模块状态 再用|停用
            _eIInfo.AddBlock(block.GetNatureBlock());//模块性质 标准版|扩展版
            _eIInfo.AddBlock(block.GetTypeBlock());//模块类型 业务|公共|基础|报表|统计
            systemModule.EIDataContext.Update(_eIInfo);

            systemModule.fdParentLevel.Value = this.fdParentLevel.Value;//父级模块级别
            systemModule.isLeaf.Value = this.isLeaf.Value;//是否叶子节点
            systemModule.fdParentId.Value = this.fdParentId.Value;//父节点ID
            int moduleLevel = Convert.ToInt16(this.fdParentLevel.Value == null ? 0 : this.fdParentLevel.Value) + 1;
            systemModule.fdModuleLevel.Value = moduleLevel;//模块级别

            systemModule.fdParentName.Value = this.fdParentName.Value;//上级模块名称
            systemModule.SetModuleLevelName(moduleLevel.ToString());//模块级别名称

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = systemModule;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(CmesWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }
    }

}
